﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using BLToolkit.ComponentModel;


namespace LED_Editor.Model
{




    public class TrackSource : BindingSourceEx
    {
        public TrackSource()
            : base()
        {
            this.DataSource = typeof(Track);
        }

        public TrackSource(IContainer container)
            : base(container)
        {
            this.DataSource = typeof(Track);
        }
        //public override void Remove(object value)
        //{
        //    base.Remove(value);
        //    ReassignAddress();

        //}

        protected override void OnListChanged(ListChangedEventArgs e)
        {
            base.OnListChanged(e);
            ReassignAddress();
        }

        private void ReassignAddress()
        {
            int currentAddress = 1;
            foreach (Track item in this)
            {
                item.NotifyChanges = false;
                item.Id = currentAddress;
                item.NotifyChanges = true;
                currentAddress++;
            }
        }
    }


    public class TrackSourceEx : ObjectBinder
    {
        public TrackSourceEx()
            : base()
        {
            this.DataSource = typeof(Track);
            InitInnerTrackSource();

        }

        private void InitInnerTrackSource()
        {
            _innerTrackSource = new TrackSource();
            _innerTrackSource.DataSource = this;
            //_innerTrackSource.CurrentChanged += new EventHandler(_innerTrackSource_CurrentChanged);
           // _innerTrackSource.PositionChanged += new EventHandler(_innerTrackSource_PositionChanged);

        }

      

        private TrackSource _innerTrackSource;

        public TrackSourceEx(IContainer container)
            : base(container)
        {
            this.DataSource = typeof(Track);
            InitInnerTrackSource();

        //   this.Container 
        }

      
        //public override void Remove(object value)
        //{
        //    base.Remove(value);
        //    ReassignAddress();

        //}
        //public override  

        public void Init()
        {
            if (DataSource == null)
                throw new InvalidOperationException();

            if (_lastUniqueId != 0)
            {
                _lastUniqueId = 0;
            }
               // throw new InvalidOperationException();


            foreach (Track item in List)
            {
                item.UniqueId = _lastUniqueId;
                _lastUniqueId++;
            }
        }

        public event EventHandler MaxFrameDurationMillisecondsChanged;
        protected void OnMaxFrameDurationMillisecondsChanged()
        {
            if (MaxFrameDurationMillisecondsChanged != null)
            {
                MaxFrameDurationMillisecondsChanged(this, EventArgs.Empty);
            }
        }

        int _previousMaxDuration;
        public int MaxFrameDurationMilliseconds
        {
            get
            {
                int maxDuration = 0;
                foreach (Track item in List)
                {
                    if (item.TotalFramesDurationMilliseconds > maxDuration)
                    {
                        maxDuration = item.TotalFramesDurationMilliseconds;
                    }
                }
                if (_previousMaxDuration != maxDuration)
                {
                    _previousMaxDuration = maxDuration;
                    OnMaxFrameDurationMillisecondsChanged();
                }
                return maxDuration;
            }
        }


        int _lastUniqueId = 0;
        protected override void OnListChanged(ListChangedEventArgs e)
        {
            base.OnListChanged(e);
            switch (e.ListChangedType)
            {
                case ListChangedType.ItemAdded:
                    Track newTrack = (Track)this.List[e.NewIndex];
                    newTrack.UniqueId = _lastUniqueId;
                    _lastUniqueId++;
                    break;
                default:
                    break;
            }
            ReassignAddress();
        }

        private void ReassignAddress()
        {
           //((EditableList)List)
            ((BLToolkit.EditableObjects.EditableArrayList)List).NotifyChanges  = false;
            int currentAddress = 1;
            foreach (Track item in this)
            {
                item.Id = currentAddress;
                currentAddress++;
            }
            ((BLToolkit.EditableObjects.EditableArrayList)List).NotifyChanges = true;

        }

       // public object Current
       // {
       //     get { return _innerTrackSource.Current; }
       // }
       // public int Position
       // {
       //     get { return _innerTrackSource.Position; }
       // }

       // public event EventHandler CurrentChanged;
       // public event EventHandler PositionChanged
       //{
       //     add { _innerTrackSource.CurrentChanged+=value ; }
       //     remove { _innerTrackSource.CurrentChanged-=value ; }
       // }

       // void _innerTrackSource_CurrentChanged(object sender, EventArgs e)
       // {
       //     OnCurrentChanged();
       // }
       // void _innerTrackSource_PositionChanged(object sender, EventArgs e)
       // {
       //     OnPositionChanged();
       // }

       // private void OnCurrentChanged()
       // {
       //     if (CurrentChanged != null)
       //     {
       //         CurrentChanged(this, EventArgs.Empty);
       //     }
       // }
       // private void OnPositionChanged()
       // {
       //     if (PositionChanged != null)
       //     {
       //         PositionChanged(this, EventArgs.Empty);
       //     }
       // }


    }
}
